<template>
  <div>
    <el-container class="full-height-container">
      <el-container>
        <!-- 侧边栏 -->
        <el-aside width="200px" class="sidebar-container">
          <div class="sidebar-header">
            <div class="sidebar-logo">
              <span class="logo-icon">📊</span>
              <span class="logo-text">浩瀚管理系统</span>
            </div>
          </div>

          <el-menu
              v-if="menus.length>0"
              :router="true"
              :default-active="activeIndex"
              class="el-menu-vertical-demo sidebar-menu"
              text-color="#bfcbd9"
              active-text-color="#409eff"
              background-color="transparent"
              :collapse-transition="true">
            <template v-for="menu in menus" :key="menu.menuId">
              <el-sub-menu
                  v-if="menu.children && menu.children.length > 0"
                  :index="menu.path"
                  class="sidebar-submenu"
              >
                <template #title>
                  <i :class="menu.icon || 'el-icon-menu'" class="menu-icon"></i>
                  <span class="menu-text">{{ menu.name }}</span>
                </template>
                <template v-for="child in menu.children" :key="child.menuId">
                  <el-menu-item
                      :index="child.path"
                      @click="handleActive(child)"
                      class="sidebar-submenu-item"
                  >
                    <i :class="child.icon || 'el-icon-circle-outline'" class="submenu-icon"></i>
                    <span class="submenu-text">{{ child.name }}</span>
                  </el-menu-item>
                </template>
              </el-sub-menu>
              <el-menu-item
                  v-else
                  :index="menu.path"
                  @click="handleActive(menu)"
                  class="sidebar-menu-item"
              >
                <i :class="menu.icon || 'el-icon-circle-outline'" class="menu-icon"></i>
                <span class="menu-text">{{ menu.name }}</span>
              </el-menu-item>
            </template>
          </el-menu>
          <p v-else class="no-menu-tip">暂无菜单数据</p>
        </el-aside>

        <!-- 主内容区 -->
        <el-container class="main-container">
          <!-- 顶部导航栏 -->
          <el-header class="header-container">
            <div class="header-content">
              <span class="header-welcome">欢迎使用 浩瀚管理系统</span>
              <!-- 用户信息区（含退出功能） -->
              <div class="user-info">
                <el-icon class="user-icon"><User /></el-icon>
                <span class="username">管理员</span>
                <!-- 退出图标添加点击事件 -->
                <el-icon
                    class="logout-icon"
                    style="margin-left: 16px; cursor: pointer;"
                    @click="handleLogout"
                >
                  <ArrowRight />
                </el-icon>
              </div>
            </div>
          </el-header>

          <!-- 主内容区 -->
          <el-main class="content-container">
            <router-view />
          </el-main>
        </el-container>
      </el-container>
    </el-container>

    <!-- 退出确认对话框 -->
    <el-dialog
        v-model="showLogoutConfirm"
        title="退出登录"
        width="300px"
        :close-on-click-modal="false"
    >
      <p style="text-align: center; margin: 20px 0;">确定要退出当前登录吗？</p>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showLogoutConfirm = false">取消</el-button>
          <el-button type="primary" @click="confirmLogout">确定退出</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { User, Menu, CircleOutline, ArrowRight } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';

const router = useRouter();
const menus = ref(JSON.parse(sessionStorage.getItem("menus")) || []);
const activeIndex = ref(
    menus.value.length > 0
        ? (menus.value[0].children && menus.value[0].children.length > 0
            ? menus.value[0].path
            : menus.value[0].path)
        : ""
);

// 退出相关状态
const showLogoutConfirm = ref(false);

const handleActive = (item) => {
  activeIndex.value = item.path;
};

// 打开退出确认对话框
const handleLogout = () => {
  showLogoutConfirm.value = true;
};

// 确认退出登录
const confirmLogout = async () => {
  try {
    // 1. 调用后端退出接口（如果需要）
    // await httpRequest.post('user/logout');

    // 2. 清除本地存储的登录状态
    sessionStorage.removeItem('token');
    sessionStorage.removeItem('userInfo');
    sessionStorage.removeItem('menus');

    // 3. 隐藏确认框
    showLogoutConfirm.value = false;

    // 4. 提示退出成功
    ElMessage.success('已成功退出登录');

    // 5. 跳转到登录页
    setTimeout(() => {
      router.push('/login');
    }, 500);
  } catch (error) {
    console.error('退出登录失败', error);
    ElMessage.error('退出登录失败，请重试');
  }
};
</script>

<style scoped>
/* 容器全屏布局 */
.full-height-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
  overflow: hidden;
}

/* 侧边栏容器 */
.sidebar-container {
  height: 100vh;
  background-color: #1f2d3d;
  transition: width 0.2s ease;
  overflow-y: auto;
}

/* 侧边栏Header */
.sidebar-header {
  height: 50px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  border-bottom: 1px solid #2d3748;
}
.sidebar-logo {
  display: flex;
  align-items: center;
}
.logo-icon {
  font-size: 20px;
  color: #409eff;
  margin-right: 8px;
}
.logo-text {
  font-size: 16px;
  font-weight: 500;
  color: #ffffff;
}

/* 侧边栏菜单 */
.sidebar-menu {
  border-right: none;
  margin-top: 10px;
}

.sidebar-menu-item {
  height: 46px;
  line-height: 46px;
  padding: 0 20px;
}
.menu-icon {
  font-size: 16px;
  margin-right: 8px;
}
.menu-text {
  font-size: 14px;
}

.sidebar-menu .el-menu-item:hover,
.sidebar-menu .el-submenu__title:hover {
  background-color: rgba(255, 255, 255, 0.05);
}
.sidebar-menu .el-menu-item.is-active {
  background-color: rgba(64, 158, 255, 0.1);
  border-right: 3px solid #409eff;
}

.sidebar-submenu .el-submenu__content {
  background-color: #273444 !important;
}
.sidebar-submenu-item {
  height: 42px;
  line-height: 42px;
  padding: 0 10px 0 40px !important;
}
.submenu-icon {
  font-size: 15px;
  margin-right: 8px;
}
.submenu-text {
  font-size: 13px;
  color: #aab7c4;
}
.sidebar-submenu-item.is-active {
  background-color: rgba(64, 158, 255, 0.15) !important;
  border-right: 3px solid #409eff;
}

.no-menu-tip {
  color: #e1e5ea;
  text-align: center;
  padding: 40px 0;
  margin: 0;
  font-size: 14px;
}

/* 主内容区容器 */
.main-container {
  background-color: #f5f6f8;
  height: 100vh;
  overflow: hidden;
}

/* 顶部导航栏 */
.header-container {
  background-color: #ffffff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  height: 50px;
  padding: 0 20px;
}
.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  font-size: 14px;
}
.header-welcome {
  color: #666;
}
.user-info {
  display: flex;
  align-items: center;
  color: #666;
  gap: 12px;
}
.user-icon, .logout-icon {
  font-size: 16px;
  color: #999;
}
.logout-icon:hover {
  color: #f56c6c; /* 鼠标悬停时变红色，增强提示 */
}
.username {
  font-weight: 500;
  color: #333;
}

/* 主内容区 */
.content-container {
  padding: 20px;
  height: calc(100vh - 50px);
  overflow-y: auto;
}
.content-container > div {
  background-color: #ffffff;
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  padding: 20px;
  height: 100%;
}

/* 对话框样式 */
.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 10px;
}
</style>
